package com.wyfc.gb;


import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration;
import org.springframework.boot.autoconfigure.h2.H2ConsoleAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration;
import org.springframework.boot.autoconfigure.session.SessionAutoConfiguration;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableScheduling;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.MemoryUsage;
import java.lang.management.OperatingSystemMXBean;

/**
 * 说明：
 * @类名: Application
 *
 * @author   kenny
 * @Date	 2021年3月11日下午2:03:15
*/
@SpringBootApplication(exclude={
		DataSourceAutoConfiguration.class,
		H2ConsoleAutoConfiguration.class,
		RedisAutoConfiguration.class,
		RedisRepositoriesAutoConfiguration.class,
		SessionAutoConfiguration.class,
		KafkaAutoConfiguration.class
})
@ComponentScan(basePackages = {"com.wyfc.gb","com.js.lib"})
@EnableScheduling
@Slf4j
public class Application {

	private static String[] args;
	private static ConfigurableApplicationContext context;

	public static void main(String[] args) throws IOException {
		Application.args = args;
		Application.context = SpringApplication.run(Application.class,args);
		log.info("========================================================");
		log.info("WYFC-GB202508 Server is running");
		log.info("========================================================");
	}

	public static void restart(){
		context.close();
		Application.context = SpringApplication.run(Application.class,args);
	}

	public static void performance(){
		OperatingSystemMXBean osBean = ManagementFactory.getOperatingSystemMXBean();

		// 获取CPU使用率，这里的实现取决于操作系统
		double cpuLoad = osBean.getSystemLoadAverage();

		// 获取内存MXBean
		MemoryMXBean memoryBean = ManagementFactory.getMemoryMXBean();
		MemoryUsage heapMemoryUsage = memoryBean.getHeapMemoryUsage();

		// 输出系统信息
		System.out.println("CPU Usage: " + cpuLoad);
		System.out.println("Heap Memory Usage:");
		System.out.println("Init: " + heapMemoryUsage.getInit() + " bytes");
		System.out.println("Used: " + heapMemoryUsage.getUsed() + " bytes");
		System.out.println("Committed: " + heapMemoryUsage.getCommitted() + " bytes");
		System.out.println("Max: " + heapMemoryUsage.getMax() + " bytes");

		// 获取总内存和可用内存
		Runtime runtime = Runtime.getRuntime();
		long totalMemory = runtime.totalMemory();
		long freeMemory = runtime.freeMemory();
		long usedMemory = totalMemory - freeMemory;

		System.out.println("Total Memory: " + totalMemory + " bytes");
		System.out.println("Used Memory: " + usedMemory + " bytes");
		System.out.println("Free Memory: " + freeMemory + " bytes");
	}
	public static void exePerformance() throws IOException {
		String exeName = "redis-server.exe"; // 替换为你的EXE文件名
		String line;
		Process process = Runtime.getRuntime().exec("tasklist");
		BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));

		while ((line = reader.readLine()) != null) {
			if (line.contains(exeName)) {
				String[] parts = line.split("\\s+"); // 使用空白字符分割行
				System.out.println("CPU Usage: " + parts[5]); // 假设CPU使用率在第六列
				System.out.println("Memory Usage: " + parts[6]); // 假设内存使用率在第七列
				break;
			}
		}
		reader.close();
		process.destroy();
	}
}
